package net.htjs.juc.thread;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

@Slf4j
public class FutureTaskThread {

    public static void main(String[] args) {
        try {
            //创建任务对象
            FutureTask<Integer> task = new FutureTask<>(() -> {
                log.info("FutureTask Running");
                Thread.sleep(2000);
                return 100;
            });

            //参数1是任务对象，参数2是线程名字
            new Thread(task, "Thread 3").start();

            //主线程阻塞，同步等待task任务执行完毕的结果
            Integer result = task.get();
            log.info("任务执行结果是:{}", result);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

}
